package metalink;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.sql.*;
import java.util.Map;
import org.apache.struts2.ServletActionContext;

import vo.*;
import vo.mixed.*;
import model.admin.*;
import myutil.*;
import java.io.*;

import javax.servlet.http.HttpSession;

public class MessageSearch extends ActionSupport{
	HttpSession session;
	private String filter;
	private String search_box;
	private static final long serialVersionUID = 1L;
ArrayList<Message_Receiver_MixedVo> unread=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> read=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> inbox=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> sent=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> deleted=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> starred=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> message_search=new ArrayList<Message_Receiver_MixedVo>();
private int inbox_count = 0;
private int message_search_count = 0;
private int read_count = 0;
private int unread_count = 0;
private int starred_count = 0;
private int deleted_count = 0;
private int sent_count = 0;
public ArrayList<Message_Receiver_MixedVo> getUnread() {
	return unread;
}
public void setUnread(ArrayList<Message_Receiver_MixedVo> unread) {
	this.unread = unread;
}
public ArrayList<Message_Receiver_MixedVo> getRead() {
	return read;
}
public void setRead(ArrayList<Message_Receiver_MixedVo> read) {
	this.read = read;
}
public String execute(){
	try {
		
		String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		Connect c=new Connect();
		ResultSet rs = null;
		if(filter.equals("To")){
			session = ServletActionContext.getRequest().getSession();
			session.setAttribute("filter", filter);
			if(search_box.length() > 20){
				session.setAttribute("search_box", search_box.substring(0, 20) + " ...");
			}
			else{
				session.setAttribute("search_box", search_box);
			}
			rs=c.getResult("select * From message m, message_receiver mr where m.message_id=mr.message_id and m.sender_id='"+username+"' and (mr.receiver_id='"+search_box+"' or mr.receiver_id like '%"+search_box+"' or mr.receiver_id like '"+search_box+"%' or mr.receiver_id like '%"+search_box+"%') and m.deleted_flag!=1 order by sent_at desc");
			while(rs.next()){
				int attachment_id,attachment_count=0;
				Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
				single_object.setMessage_id(rs.getInt("message_id"));
				single_object.setSubject(rs.getString("subject"));
				single_object.setBody(rs.getString("body"));
				single_object.setSender_id(rs.getString("sender_id"));
				single_object.setSent_at(rs.getTimestamp("sent_at").toString());
				single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
				single_object.setRead_flag(rs.getInt("read_flag"));
				single_object.setStarred_flag(rs.getInt("m.starred_flag"));
				attachment_id=rs.getInt("attachment_id");
				File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
				if(attachment_id!=0)
					attachment_count=f.list().length;
				single_object.setAttachment_count(attachment_count);
				message_search.add(single_object);
				message_search_count = message_search.size();
			}
		}
		else if(filter.equals("From")){
			session = ServletActionContext.getRequest().getSession();
			session.setAttribute("filter", filter);
			if(search_box.length() > 20){
				session.setAttribute("search_box", search_box.substring(0, 20) + " ...");
			}
			else{
				session.setAttribute("search_box", search_box);
			}
			rs=c.getResult("select * From message m, message_receiver mr where m.message_id=mr.message_id and mr.receiver_id='"+username+"' and (m.sender_id='"+search_box+"' or m.sender_id like'%"+search_box+"' or m.sender_id like '"+search_box+"%' or m.sender_id like '%"+search_box+"%') and mr.deleted_flag!=1 order by sent_at desc");
			while(rs.next()){
				int attachment_id,attachment_count=0;
				Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
				single_object.setMessage_id(rs.getInt("message_id"));
				single_object.setSubject(rs.getString("subject"));
				single_object.setBody(rs.getString("body"));
				single_object.setSender_id(rs.getString("sender_id"));
				single_object.setSent_at(rs.getTimestamp("sent_at").toString());
				single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
				single_object.setRead_flag(rs.getInt("read_flag"));
				single_object.setStarred_flag(rs.getInt("m.starred_flag"));
				attachment_id=rs.getInt("attachment_id");
				File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
				if(attachment_id!=0)
					attachment_count=f.list().length;
				single_object.setAttachment_count(attachment_count);
				message_search.add(single_object);
				message_search_count = message_search.size();
			}
		}
		
		else if(filter.equals("Subject")){
			session = ServletActionContext.getRequest().getSession();
			session.setAttribute("filter", filter);
			if(search_box.length() > 20){
				session.setAttribute("search_box", search_box.substring(0, 20) + " ...");
			}
			else{
				session.setAttribute("search_box", search_box);
			}
			rs=c.getResult("select * From message m, message_receiver mr where m.message_id=mr.message_id and (m.sender_id='"+username+"' or mr.receiver_id='"+username+"') and (m.subject='"+search_box+"' or m.subject like '%"+search_box+"' or m.subject like '"+search_box+"%' or m.subject like '%"+search_box+"%') and mr.deleted_flag!=1 order by sent_at desc");
			while(rs.next()){
				int attachment_id,attachment_count=0;
				Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
				single_object.setMessage_id(rs.getInt("message_id"));
				single_object.setSubject(rs.getString("subject"));
				single_object.setBody(rs.getString("body"));
				single_object.setSender_id(rs.getString("sender_id"));
				single_object.setSent_at(rs.getTimestamp("sent_at").toString());
				single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
				single_object.setRead_flag(rs.getInt("read_flag"));
				single_object.setStarred_flag(rs.getInt("m.starred_flag"));
				attachment_id=rs.getInt("attachment_id");
				File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
				if(attachment_id!=0)
					attachment_count=f.list().length;
				single_object.setAttachment_count(attachment_count);
				message_search.add(single_object);
				message_search_count = message_search.size();
			}
		}
		
		else if(filter.equals("Body")){
			session = ServletActionContext.getRequest().getSession();
			session.setAttribute("filter", filter);
			if(search_box.length() > 20){
				session.setAttribute("search_box", search_box.substring(0, 20) + " ...");
			}
			else{
				session.setAttribute("search_box", search_box);
			}
			rs=c.getResult("select * From message m, message_receiver mr where m.message_id=mr.message_id and (m.sender_id='"+username+"' or mr.receiver_id='"+username+"') and (m.body='"+search_box+"'or m.body like '%"+search_box+"'or m.body like '"+search_box+"%'or m.body like '%"+search_box+"%') and mr.deleted_flag!=1 order by sent_at desc");
			while(rs.next()){
				int attachment_id,attachment_count=0;
				Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
				single_object.setMessage_id(rs.getInt("message_id"));
				single_object.setSubject(rs.getString("subject"));
				single_object.setBody(rs.getString("body"));
				single_object.setSender_id(rs.getString("sender_id"));
				single_object.setSent_at(rs.getTimestamp("sent_at").toString());
				single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
				single_object.setRead_flag(rs.getInt("read_flag"));
				single_object.setStarred_flag(rs.getInt("m.starred_flag"));
				attachment_id=rs.getInt("attachment_id");
				File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
				if(attachment_id!=0)
					attachment_count=f.list().length;
				single_object.setAttachment_count(attachment_count);
				message_search.add(single_object);
				message_search_count = message_search.size();
			}
		}
		
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=0 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			unread.add(single_object);
			unread_count = unread.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=1 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			read.add(single_object);
			read_count = read.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			inbox.add(single_object);
			inbox_count = inbox.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			deleted.add(single_object);
			deleted_count = deleted.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag!=1 and mr.starred_flag=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			starred.add(single_object);
			starred_count = starred.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and m.sender_id='"+username+"' and m.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("m.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			sent.add(single_object);
			sent_count = sent.size();
		}
		return SUCCESS;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return 	ERROR;
	}
}
public ArrayList<Message_Receiver_MixedVo> getInbox() {
	return inbox;
}
public void setInbox(ArrayList<Message_Receiver_MixedVo> inbox) {
	this.inbox = inbox;
}
public ArrayList<Message_Receiver_MixedVo> getSent() {
	return sent;
}
public void setSent(ArrayList<Message_Receiver_MixedVo> sent) {
	this.sent = sent;
}
public int getInbox_count() {
	return inbox_count;
}
public void setInbox_count(int inbox_count) {
	this.inbox_count = inbox_count;
}
public int getRead_count() {
	return read_count;
}
public void setRead_count(int read_count) {
	this.read_count = read_count;
}
public int getUnread_count() {
	return unread_count;
}
public void setUnread_count(int unread_count) {
	this.unread_count = unread_count;
}
public int getStarred_count() {
	return starred_count;
}
public void setStarred_count(int starred_count) {
	this.starred_count = starred_count;
}
public int getDeleted_count() {
	return deleted_count;
}
public void setDeleted_count(int deleted_count) {
	this.deleted_count = deleted_count;
}
public int getSent_count() {
	return sent_count;
}
public void setSent_count(int sent_count) {
	this.sent_count = sent_count;
}
public ArrayList<Message_Receiver_MixedVo> getDeleted() {
	return deleted;
}
public void setDeleted(ArrayList<Message_Receiver_MixedVo> deleted) {
	this.deleted = deleted;
}
public ArrayList<Message_Receiver_MixedVo> getStarred() {
	return starred;
}
public void setStarred(ArrayList<Message_Receiver_MixedVo> starred) {
	this.starred = starred;
}
public String getFilter() {
	return filter;
}
public void setFilter(String filter) {
	this.filter = filter;
}
public String getSearch_box() {
	return search_box;
}
public void setSearch_box(String search_box) {
	this.search_box = search_box;
}
public ArrayList<Message_Receiver_MixedVo> getMessage_search() {
	return message_search;
}
public void setMessage_search(ArrayList<Message_Receiver_MixedVo> message_search) {
	this.message_search = message_search;
}
public int getMessage_search_count() {
	return message_search_count;
}
public void setMessage_search_count(int message_search_count) {
	this.message_search_count = message_search_count;
}
}
